{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/python\n",
    "# -*- coding: UTF-8 -*-\n",
    "\n",
    "import os\n",
    "import os.path\n",
    "\n",
    "#指定要处理的文件目录，并列出文件名称\n",
    "files_dir = r'F:\\在做工作\\0905\\工程院文献下载\\中文专利'\n",
    "file_list = os.listdir(files_dir)\n",
    "print(file_list,type(file_list))\n",
    "\n",
    "#打开要存储数据的txt文件，采用utf-8编码，在后追加数据的形式\n",
    "fobj=open(r'F:\\Patent_resutl.txt','a',encoding='utf-8')\n",
    "\n",
    "#循环文件名，每个文件做处理\n",
    "for file_name in file_list:\n",
    "    \n",
    "    #新开一个文件，需要清空原始获取的数据列表\n",
    "    data = []\n",
    "    \n",
    "    #循环打开文件，并读取每一行，获取全部数据到列表\n",
    "    for line in open(files_dir + '\\\\' + file_name,\"r\",encoding='UTF-8'): #设置文件对象并读取每一行文件\n",
    "        data.append(line.split('=',1))#将读取出来的每个文件中的数据，按照第一个等号分隔，并追加到data列表中\n",
    "    \n",
    "    line_string = ''#建立一个空的字符串变量\n",
    "    #按行获取data\n",
    "    for line_value in data:\n",
    "        #检查是否每一行都有数据，只有表头的内容写入空数据后跳过，将获取到有数据的的数据写入文件，随后清空数据行\n",
    "        if (len(line_value)==1): \n",
    "            fobj.write('\\n' + line_string)\n",
    "            line_string = ''\n",
    "            continue\n",
    "        #清空所有换行符,并且将分隔符修改为<>\n",
    "        line_string = line_string + '|' + line_value[1].replace('\\n','').replace('\\r','')\n",
    "#关闭txt文件\n",
    "fobj.close() \n",
    "\n",
    "print('done')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
